﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HPAGMRestAPIWrapper;

namespace AGMRestAPITest
{
    /// <summary>
    /// Summary description for AGMTestSetTest
    /// </summary>
    [TestClass]
    public class AGMTestSetTest
    {
        public AGMTestSetTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext _testContextInstance;

        private static AGMConnection _connection;
        private static AGMTestSetFolders _testSetFolders;
        private static AGMTestSets _testSets;

        private AGMTestSetFolder _testSetFolder;
        private AGMTestFolder _testFolder;
        private AGMTest _test1;
        private AGMTest _test2;
        private AGMTestSet _testSet;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void MyClassInitialize(TestContext testContext)
        {
            _connection = new AGMConnection(AGMSetting.SERVERNAME, AGMSetting.USERNAME, AGMSetting.PASSWORD);
            _testSetFolders = new AGMTestSetFolders(_connection);
            _testSets = new AGMTestSets(_connection);
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void MyClassCleanup()
        {
            if (_connection != null)
                _connection.Logoff();
        }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize]
        public void MyTestInitialize()
        {
            #region prepare for test instance
            var testfolders = new AGMTestFolders(_connection);
            _testFolder = testfolders.Get(AGMSetting.ROOT_TESTFOLDER_ID).CreateFolder("AGMREST Test Folder");

            _test1 = _testFolder.AddTest("New Test 1");
            _test2 = _testFolder.AddTest("New Test 2");
            #endregion

            #region Add test set folder
            _testSetFolder = _testSetFolders.Get(AGMSetting.ROOT_TESTSETFOLDER_ID).CreateFolder("Test Set Folder");
            Assert.IsTrue(_testSetFolders.IsExists(_testSetFolder.Id.Value), "Create test set folder failed.");

            _testSet = _testSetFolder.AddTestSet("New Test Set");
            #endregion
        }

        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            _test1.Delete();
            _test2.Delete();
            _testFolder.Delete();
            _testSet.Delete();
            _testSetFolder.Delete();
            Assert.IsTrue(!_testSetFolders.IsExists(_testSetFolder.Id.Value), "delete test set folder fail.");
        }
        //
        #endregion

        [TestMethod]
        public void TestSet_UpdateFolder()
        {
            #region Update test set folder
            _testSetFolder.SetUpdateField("description", "Description of Test Set Folder");
            var updFolder = _testSetFolder.Update();
            Assert.AreEqual(updFolder.GetField("description").Value, "Description of Test Set Folder", "Update test folder failed.");
            #endregion
        }

        [TestMethod]
        public void TestSet_Update()
        {
            #region update test set item
            _testSet.SetUpdateField("status","Closed");
            var updatedTestSet = _testSet.Update();
            Assert.AreEqual(updatedTestSet.GetField("status").Value, "Closed", "Update test set fail.");
            #endregion
        }

        [TestMethod]
        public void TestSet_AddTestInstances()
        {
            _testSet.AddTests(new List<AGMTest> { _test1, _test2 });
            var testInstances = _testSet.GetTestInstances();
            foreach (var ti in testInstances)
            {
                Assert.IsTrue((ti.GetField("test-id").Value.Equals(_test1.Id.Value.ToString()) || ti.GetField("test-id").Value.Equals(_test2.Id.Value.ToString())));
            }
        }

        [TestMethod]
        public void TestSet_UploadDownLoadDeleteAttachmentOnFolder()
        {
            _testSetFolder.UploadAttachment(@"Test.txt");
            Assert.IsTrue(_testSetFolder.IsAttachmentExists("Test.txt"), "Uploaded attachement is not exists.");

            _testSetFolder.DownloadAttachment(@"Test.txt", @"c:\Test.txt");
            Assert.IsTrue(Helper.CheckAttachment(@"C:\Test.txt"), "Downloaded attachement is not exists.");

            _testSetFolder.DeleteAttachment(@"Test.txt");
            Assert.IsFalse(_testSetFolder.IsAttachmentExists("Test.txt"), "Uploaded attachement is not exists.");
        }

        [TestMethod]
        public void TestSet_UploadDownLoadDeleteAttachment()
        {
            _testSet.UploadAttachment(@"Test.txt");
            Assert.IsTrue(_testSet.IsAttachmentExists("Test.txt"), "Uploaded attachement is not exists.");

            _testSet.DownloadAttachment(@"Test.txt", @"c:\Test.txt");
            Assert.IsTrue(Helper.CheckAttachment(@"C:\Test.txt"), "Downloaded attachement is not exists.");

            _testSet.DeleteAttachment(@"Test.txt");
            Assert.IsFalse(_testSet.IsAttachmentExists("Test.txt"), "Uploaded attachement is not exists.");
        }
    }
}
